package com.honeybee.cloud.framework.common.interceptor;

import com.honeybee.cloud.framework.base.entity.HeaderSubject;
import com.honeybee.cloud.framework.base.enums.HeaderEnum;
import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.base.exception.business.BaseBizExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 头部拦截器
 * @Author DELL
 * @Create 2018-12-08
 * @Since 1.0.0
 */
@Slf4j
public class HeaderInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) {

        log.info("开始头部拦截器流程");
        if (!(handler instanceof HandlerMethod)) {
            log.info("不需要处理头部拦截器");
            return true;
        }
        log.info("开始判断请求头部信息");

        String appKey = request.getHeader(HeaderEnum.APP_KEY.getCode());
        log.info("请求头部信息:{},{}", HeaderEnum.APP_KEY.getCode(), appKey);
        if (StringUtils.isBlank(appKey)) {
           throw  new BaseBizException(BaseBizException.getInstance(BaseBizExceptionEnum.HEADER_NOT_EXISTS));
        }
        HeaderSubject subject=new HeaderSubject();
        subject.setAppKey(appKey);
        log.info("头部拦截器流程处理完成");
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object handler, Exception e) {
    }


}
